﻿using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;
using BitMiracle.LibJpeg;

namespace DesktopCapture
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            tmDC.Tick += tmDC_Tick;
        }

        void tmDC_Tick(object sender, EventArgs e)
        {
            picDesktop.Image = DCapture.Capture();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            tmDC.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            btnStop.Enabled = false;
            btnStart.Enabled = true;
            tmDC.Stop();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = false;
            tmDC.Stop();
            picDesktop.Image.Save("des.bmp");
            MessageBox.Show("Save Bmp Done");
            btnSave.Enabled = true;
        }

        private void btnSaveJpeg_Click(object sender, EventArgs e)
        {
            int quality;
            if (!int.TryParse(txtQuality.Text, out quality)) return;
            var ji = new JpegImage((Bitmap) picDesktop.Image);
            var cp = new CompressionParameters();
            cp.Quality = quality;
            Task.Factory.StartNew(() => ji.Save("des.jpg", cp))
                .ContinueWith(t => MessageBox.Show("Save Jpeg Done"));            
        }        
    }
}
